<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .map {
      position: relative;
      width: 800px;
      height: 600px;
      background-color: #ccc;
    }
  </style>
</head>
<body>
  <div class="map"></div>
  <script>
  // 获取地图
    var map = document.querySelector('.map');
    (function () {
      var elements = [];
      function Food(width, height, color) {
        this.width = width || 20;
        this.height = height || 20;
        this.color = color || 'orange';
      }
      Food.prototype.init = function (map) {
        this.remove();
        var cib = document.createElement('div');
        cib.style.position = 'absolute';
        cib.style.width = this.width + 'px';
        cib.style.height = this.height + 'px';
        cib.style.backgroundColor = this.color;
        this.x = Math.floor(Math.random() * (map.offsetWidth) / this.width) * this.width;
        this.y = Math.floor(Math.random() * (map.offsetHeight) / this.height) * this.height;
        cib.style.top = this.y + 'px';
        cib.style.left = this.x + 'px';
        map.appendChild(cib);
        elements.push(cib);
      }
      Food.prototype.remove = function () {
        for(var i = 0; i < elements.length; i++) {
          var ele = elements[i];
          ele.parentNode.removeChild(ele);
          elements.splice(i,1);
        }
      }
      window.Food = Food;
    }());
    (function () {
      var elements = [];
      function Snake(width, height, direction) {
        this.width = width || 20;
        this.height = height || 20;
        this.direction = direction || 'right';
        this.body = [
          {
            x: 3,
            y: 2,
            color: 'red'
          },
          {
            x: 2,
            y: 2,
            color: 'orange'
          },
          {
            x: 1,
            y: 2,
            color: 'orange'
          }
        ];
      }
      Snake.prototype.init = function (map) {
        this.remove();
        for(var i = 0; i < this.body.length; i++) {
          var div = document.createElement('div');
          div.style.position = 'absolute';
          div.style.left = this.body[i].x * this.width + 'px';
          div.style.top = this.body[i].y * this.height + 'px';
          div.style.width = this.width + 'px';
          div.style.height = this.height + 'px';
          div.style.backgroundColor = this.body[i].color;
          map.appendChild(div);
          elements.push(div);
        };
      }
      Snake.prototype.moveSnake = function (food,map) {
        for(var i = this.body.length - 1; i > 0; i--) {
          this.body[i].x = this.body[i - 1].x;
          this.body[i].y = this.body[i - 1].y;
        }
        switch(this.direction) {
          case 'left':
          this.body[0].x -= 1;
          break;
          case 'top':
          this.body[0].y -= 1;
          break;
          case 'right':
          this.body[0].x += 1;
          break;
          case 'bottom':
          this.body[0].y += 1;
          break;
        }
        var headX = this.body[0].x * this.width;
        var headY = this.body[0].y * this.height;
        if(headX === food.x && headY === food.y) {
          var last = this.body[this.body.length - 1];
          this.body.push({
            x: last.x,
            y: last.y,
            color: last.color
          })
          food.init(map);
        }
      }
      Snake.prototype.remove = function () {
        for(var i = elements.length - 1; i >= 0; i--) {
          var ele = elements[i];
          ele.parentNode.removeChild(ele);
          elements.splice(i, 1);
        }
      };
      window.Snake = Snake;
    }());
    (function () {
      function Game(map) {
        this.food = new Food();
        this.snake = new Snake();
        this.map = map;
        this.speed = 170;
      }
      Game.prototype.init = function () {
        this.food.init(this.map);
        this.snake.init(this.map);
        this.runSnake(this.map,this.food);
        this.bindKey();
      }
      Game.prototype.runSnake = function (map,food) {
        clearInterval(this.timeId);
        this.timeId = setInterval(function () {
          this.snake.moveSnake(food,map);
          this.snake.init(map);
          var maxX = map.offsetWidth / this.snake.width;
          var maxY = map.offsetHeight / this.snake.height;
          var headX = this.snake.body[0].x;
          var headY = this.snake.body[0].y;
          if(headX < 0 || headX > maxX || headY < 0 || headY > maxY) {
            clearInterval(this.timeId);
            alert('Game over');
          }
        }.bind(this), this.speed);
      }
      Game.prototype.bindKey = function () {
        document.addEventListener('keydown',function (event) {
          switch(event.keyCode) {
            case 37:
            this.snake.direction = 'left';
            break;
            case 38:
            this.snake.direction = 'top';
            break;
            case 39:
            this.snake.direction = 'right';
            break;
            case 40:
            this.snake.direction = 'bottom';
            break;
            case 32:
            game.spped = game.speed > 10 ? game.speed -= 10 : 20;
            game.runSnake(game.map,game.food);
            break;
          }
          console.log(event.keyCode)
        }.bind(this),false);
      }
      window.Game = Game;
    }());
    var game = new Game(map);
    game.init();
  </script>
</body>
</html>
